package hiatao.puzzle.activity;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;

import haitao.puzzle.R;
import hiatao.puzzle.camera.CameraContainer;
import hiatao.puzzle.camera.CameraView.FlashMode;
import hiatao.puzzle.inter.SaveImageCallback;
import hiatao.puzzle.utils.FileUtils;
import hiatao.puzzle.utils.PixelUtil;
import hiatao.puzzle.utils.StatusBarCompat;


/**   
* @Package hiatao.puzzle.activity 
* @Description: 相机
* @author haitao
* @date 2015-4-12 下午10:23:52 
* @version V1.0   
*/
public class CameraActivity extends Activity implements View.OnClickListener{
	public final static String TAG="CameraAty";
	private CameraContainer mContainer;
	private ImageButton takePicBtn;
	private ImageButton cancelBtn;
	private ImageButton confirmBtn;
	private ImageView flashView;
	private ImageView switchCameraView;
	private View bottomView;
	private String picturePath;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
		setContentView(R.layout.camera);
		bottomView=findViewById(R.id.camera_bottom_bar);
		mContainer=(CameraContainer)findViewById(R.id.container);
		takePicBtn=(ImageButton)findViewById(R.id.take_picture);
		cancelBtn=(ImageButton)findViewById(R.id.cancel);
		confirmBtn=(ImageButton)findViewById(R.id.confirm);
		switchCameraView=(ImageView)findViewById(R.id.btn_switch_camera);
		flashView=(ImageView)findViewById(R.id.btn_flash_mode);

		LayoutParams lp = (LayoutParams) bottomView.getLayoutParams();
		lp.height=(PixelUtil.getScreenHeight(this)-PixelUtil.getScreenWidth(this)-PixelUtil.dp2px(this, 50));
		takePicBtn.setOnClickListener(this);
		flashView.setOnClickListener(this);
		switchCameraView.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);
		confirmBtn.setOnClickListener(this);
		StatusBarCompat.compat(this, getResources().getColor(R.color.primary));
	}



	@Override
	public void onClick(View view) {
		
		switch (view.getId()) {
		case R.id.take_picture:
			takePicBtn.setClickable(false);
			confirmBtn.setVisibility(View.INVISIBLE);
			mContainer.getCameraView().setPreview(true);
			mContainer.takePicture(new PictureCallback() {
				
				@Override
				public void onPictureTaken(byte[] data, final Camera camera) {
				
					FileUtils.saveFileNewThread(CameraActivity.this, data,new SaveImageCallback() {

						@Override
						public void saveResult(final boolean reslut, final String allPath) {
							runOnUiThread(new Runnable()    
					        {    
					            public void run()    
					            {    
					            	// 失败 重启相机
					            	if(!reslut){
					            		camera.startPreview();
					            		confirmBtn.setVisibility(View.INVISIBLE);
					            	}
					            	takePicBtn.setClickable(true);
					            	confirmBtn.setVisibility(View.VISIBLE);
					            	picturePath =  allPath;
					            } 
					        });
						}
					});
					//camera.startPreview();
					//
				}
			},null);
			break;

		case R.id.btn_flash_mode:
			if(mContainer.getFlashMode()==FlashMode.ON){
				mContainer.setFlashMode(FlashMode.OFF);
				flashView.setImageResource(R.drawable.btn_flash_off);
			}else if (mContainer.getFlashMode()==FlashMode.OFF) {
				mContainer.setFlashMode(FlashMode.AUTO);
				flashView.setImageResource(R.drawable.btn_flash_auto);
			}
			else if (mContainer.getFlashMode()==FlashMode.AUTO) {
				mContainer.setFlashMode(FlashMode.TORCH);
				flashView.setImageResource(R.drawable.btn_flash_torch);
			}
			else if (mContainer.getFlashMode()==FlashMode.TORCH) {
				mContainer.setFlashMode(FlashMode.ON);
				flashView.setImageResource(R.drawable.btn_flash_on);
			}
			break;
		case R.id.btn_switch_camera:
			mContainer.switchCamera();
			break;
		case R.id.cancel:
			mContainer.getCameraView().getCamera().startPreview();
			confirmBtn.setVisibility(View.VISIBLE);
			mContainer.getCameraView().setPreview(false);
			break;
		case R.id.confirm:
			Intent intent = new Intent();
			intent.setClass(CameraActivity.this, PhotoProcessActivity.class);
			intent.putExtra("URL", "file:///"+picturePath);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
	}
	@Override
	protected void onResume() {		
		super.onResume();
	}
}